package com.db.logging;

import java.util.Date;

/**
 * @author Alexander Kotov (kotov.alex.22@gmail.com)
 */
public class Logger {

    private final String loggerName;
    private final Appender appender;

    public Logger(final String loggerName) {
        this.loggerName = loggerName;
        this.appender = LogAppender.getInstance();
    }

    public LogLevel getLogLevel() {
        return appender.getLogLevel();
    }

    public void log(LogLevel level, String format, Object... args) {

        String message = format;
        int i = 0;
        int index = message.indexOf("{}");
        while (index != -1) {
            String replaceStr = (i < args.length) ? args[i].toString() : "";
            message = message.replaceFirst("\\{\\}", replaceStr);
            index = message.indexOf("{}", index);
            i++;
        }
        appender.append(new LogRecord(new Date(), loggerName, Thread.currentThread().getName(), level, message));
    }

    public String getLoggerName() {
        return new String(loggerName);
    }

}
